import SwiftUI

struct BannerCarouselView: View {
    let banners: [BannerItem]
    @State private var currentIndex = 0
    
    var body: some View {
        TabView(selection: $currentIndex) {
            ForEach(Array(banners.enumerated()), id: \.element.id) { index, banner in
                AsyncImage(url: URL(string: banner.fullPicUrl)) { image in
                    image
                        .resizable()
                        .aspectRatio(contentMode: .fill)
                } placeholder: {
                    Rectangle()
                        .fill(Color.gray.opacity(0.3))
                        .overlay(
                            ProgressView()
                                .progressViewStyle(CircularProgressViewStyle())
                        )
                }
                .clipped()
                .cornerRadius(12)
                .onTapGesture {
                    if let urlString = banner.url, let url = URL(string: urlString) {
                        // Handle banner tap - could open URL or navigate
                        print("Banner tapped: \(url)")
                    }
                }
                .tag(index)
            }
        }
        .tabViewStyle(PageTabViewStyle(indexDisplayMode: .automatic))
        .onAppear {
            // Auto-scroll timer could be added here if needed
        }
    }
}

struct BannerCarouselView_Previews: PreviewProvider {
    static var previews: some View {
        BannerCarouselView(banners: [
            BannerItem(id: 1, title: "Sample Banner", pic: "https://example.com/banner1.jpg", url: "https://example.com", status: true, sort: 1, createTime: "2024-01-01", info: "Sample banner info"),
            BannerItem(id: 2, title: "Sample Banner 2", pic: "https://example.com/banner2.jpg", url: "https://example.com", status: true, sort: 2, createTime: "2024-01-01", info: "Sample banner info 2")
        ])
        .frame(height: 180)
        .padding()
    }
}